package sk.uniza.fri.cerviix.server.game;

import java.util.Random;

public class Worm
{
	private static final float SPEED_MIN = 1.0f, SPEED_MAX = 1.6f;
	private Random random = new Random();
	
	private boolean alive = true;
	private int partsCount = 0;
	
	private Vector position;
	private Vector vector;
	
	private byte direction;
	private float angle = 0.0f;
	private float speedMul = 1.0f;
	
	public Worm(Vector position, float angle)
	{
		this.position = position;
		this.angle = angle;
		
		vector = new Vector(0);
	}
	
	public void update()
	{
		switch (direction)
		{
			case -1:
				angle += 0.2f;
				break;
			case 1:
				angle -= 0.2f;
		}
		
		vector.change(angle);
		
		float speedChange = (random.nextFloat() - 0.5f) / 7;
		speedMul += speedChange;
		
		if (speedMul < SPEED_MIN)
		{
			speedMul = SPEED_MIN;
		}
		
		if (speedMul > SPEED_MAX)
		{
			speedMul = SPEED_MAX;
		}
		
		vector.scalar(speedMul);
		
		position.add(vector);
	}
	
	public void kill()
	{
		alive = false;
	}
	
	public boolean isAlive()
	{
		return alive;
	}
	
	public void setDirection(byte direction)
	{
		this.direction = direction;
	}
	
	public Vector getPosition()
	{
		return position;
	}
	
	public void partAdded()
	{
		partsCount++;
	}
	
	public enum Direction
	{
		LEFT(-1),
		STRAIGHT(0),
		RIGHT(1);
		
		public final byte value;
		
		private Direction(int value)
		{
			this.value = (byte)value;
		}
	}
	
	public int partsCount()
	{
		return partsCount;
	}
}
